package com.jeecg.p3.system.poster.web;


import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.entity.Product;
import com.jeecg.p3.system.poster.entity.ProductCoupon;
import com.jeecg.p3.system.poster.service.ProductCouponService;
import com.jeecg.p3.system.service.ProductService;
import com.jeecg.p3.system.vo.LoginUser;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 *
 * @ClassName: ProductCouponController
 * @Description: TODO 课程优惠券控制层
 * @author: zhangxiao
 * @date: 2020年11月30日 下午7:29:38
 */
@Controller
@RequestMapping("/system/back/poster/coupon")
public class ProductCouponController extends BaseController {
    @Autowired
    private ProductCouponService service;
    @Autowired
    private ProductService productService;

    /**
     *
     * @param query 优惠券查询对象
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute ProductCoupon query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<ProductCoupon> pageQuery = new PageQuery<ProductCoupon>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        Boolean isAdmin = isAdmin(request);
        if(isMobile(request)) {
            String viewName = "system/back/poster/h5_coupon.html";
            ViewVelocity.view(request,response,viewName,velocityContext);
        } else {
            if(!isAdmin) {
                query.setCreateUser(getCurrentUser(request).getId());
            }
            pageQuery.setQuery(query);
            velocityContext.put("coupon",query);
            velocityContext.put("pageInfos", SystemTools.convertPaginatedList(service.queryPageList(pageQuery)));
            String viewName = "system/back/poster/coupon.vm";
            ViewVelocity.view(request,response,viewName,velocityContext);
        }
    }

    /**
     * 跳转到新增优惠券界面页面
     * @return
     */
    @RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        PageQuery<Product> pageQuery = new PageQuery<Product>();
        pageQuery.setPageNo(1);
        pageQuery.setPageSize(999999);
        pageQuery.setQuery(new Product());
        List<Product> list = productService.queryPageList(pageQuery).getValues();
        velocityContext.put("products", list);
        String viewName = "system/back/poster/coupon_add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存信息
     * @return
     */
    @RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute ProductCoupon coupon, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            coupon.setCreateDate(DateUtil.dateToString(new Date()));
            coupon.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            coupon.setCreateUser(user.getId());
            coupon.setStatus("1");
            service.doAdd(coupon);
            j.setMsg("保存成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value="/toEdit",method = RequestMethod.GET)
    public void toEdit(@RequestParam(required = true, value = "id" ) Long id,HttpServletResponse response,HttpServletRequest request) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        ProductCoupon coupon = service.findById(id);
        PageQuery<Product> pageQuery = new PageQuery<Product>();
        pageQuery.setPageNo(1);
        pageQuery.setPageSize(999999);
        pageQuery.setQuery(new Product());
        List<Product> list = productService.queryPageList(pageQuery).getValues();
        velocityContext.put("products", list);
        velocityContext.put("coupon", coupon);
        String viewName = "system/back/poster/coupon_edit.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存修改数据
     * @return
     */
    @RequestMapping(value = "/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doEdit(@ModelAttribute ProductCoupon coupon, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            coupon.setUpdateDate(DateUtil.dateToString(new Date()));
            coupon.setUpdateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            coupon.setUpdateUser(user.getId());
            this.service.doEdit(coupon);
            j.setMsg("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("编辑失败");
        }
        return j;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value="/doDelete",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) Long id, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            ProductCoupon coupon = service.findById(id);
            coupon.setDeleteDate(DateUtil.dateToString(new Date()));
            coupon.setDeleteTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            coupon.setDeleteUser(user.getId());
            service.doDelete(coupon);
            j.setMsg("删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("删除失败:"+e.getMessage());
        }
        return j;
    }

}
